#include <iostream>
#include <cstring>
using namespace std;
// #define ONLINE_GUDGE
// #define DEBUG
using PII = pair<int, int>;

#define x first 
#define y second 
const int N = 1010, M = N*N;
const int dx[4] = {-1, 0, 0, 1};
const int dy[4] = { 0,-1, 1, 0};

int n, m; 
int mp[N][N];
PII q[M];
int B[N][N]; // 判重 state // B[i][j] : (i, j)到最近的1的距离
int vis[N][N];
int hh = 0, tt = 0;



void bfs()
{

    // bfs + 记忆化搜索
    
    while(hh <= tt)
    {
        auto t = q[hh++]; // 出队

        for(int k = 0; k < 4; k++){
            int nx = t.x + dx[k], ny = t.y + dy[k];
            if(nx < 0 || nx > n || ny < 0 || ny > m || mp[nx][ny] || B[nx][ny]) continue;     
            B[nx][ny] = B[t.x][t.y] + 1;
            q[++tt] = {nx, ny};
        }

        #ifdef DEBUG
        printf("--------------------(cx:%d, cy:%d)------------------\n", t.x, t.y);
        for(int i = 0; i < n; i++)
        {
            for(int j = 0; j < m; j++)
                printf("%d ", B[i][j]);
            printf("\n");
        }   
        printf("----------------------------------------------------\n");
        #endif
    }
}

int main()
{

    #ifdef ONLINE_JUDGE

    #else
    freopen("./in.txt","r",stdin);
    #endif
    ios::sync_with_stdio(false);   
	cin.tie(0);

    cin >> n >> m;
    
    // 扫描图
    for(int i = 0; i < n; i++)
    {
        
        for(int j = 0; j < m; j++)
            {
                char ch; cin >> ch;
                mp[i][j] = ch - '0';
                if(mp[i][j]) q[tt++] = {i,j};
            }
    }    

    

    tt--; // 保证队尾指向队列的最后一个元素

    #ifdef DEBUG
    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < m; j++)
            printf("%d ", mp[i][j]);
        printf("\n");
    } 

    for(int i = hh; i < tt; i++)
        printf("(%d %d) ", q[i].x, q[i].y);
    printf("\n");
    #endif

    bfs();
    
    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < m; j++)
            printf("%d ", B[i][j]);
        printf("\n");
    }    


    return 0;
}